package com.mla.fastdfs.filter;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class FilterHandle {

    @Autowired
    @Lazy
    private ShiroFilterFactoryBean shiroFilterFactoryBean;

    /**
     * 过滤免登录权限
     *
     * @param path
     * @return
     */
    public boolean filterPath(String path) {
        Map<String, String> filterChainDefinitionMap = shiroFilterFactoryBean.getFilterChainDefinitionMap();
        //过滤免登录验证
        for (String key : filterChainDefinitionMap.keySet()) {
            String val = filterChainDefinitionMap.get(key);
            if (val.contains("anon")) {
                String keyWith;
                if (key.startsWith("/**/")) {
                    keyWith = key.replace("/**/", "/");
                    if (path.endsWith(keyWith)) {
                        return true;
                    }
                }
                if (key.endsWith("/**")) {
                    keyWith = key.replace("/**", "");
                    if (path.startsWith(keyWith)) {
                        return true;
                    }
                }
                if (key.equals(path)) {
                    return true;
                }
            }
        }
        return false;
    }
}
